package com.zka.crm.workbench.web.controller;

import com.zka.crm.common.Constant.Constant;
import com.zka.crm.common.domain.ReturnObject;
import com.zka.crm.common.utils.DateUtils;
import com.zka.crm.common.utils.UUIDUtils;
import com.zka.crm.settings.domain.User;
import com.zka.crm.settings.service.UserService;
import com.zka.crm.workbench.domain.Activities;
import com.zka.crm.workbench.service.ActivityService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ActivityController {
    private UserService userService;

    private ActivityService activityService;

    public ActivityController(UserService userService, ActivityService activityService) {
        this.userService = userService;
        this.activityService = activityService;
    }

    @RequestMapping("/workbench/activity/index.do")
    public String index(HttpServletRequest request){
        List<User> users = userService.queryAllUsers();
        request.setAttribute("userList",users);
        return "workbench/activity/index";
    }

    @RequestMapping("/workbench/activity/saveActivity.do")
    public @ResponseBody Object saveActivity(Activities activity, HttpSession session){
        User user = (User)session.getAttribute(Constant.SESSION_USER);
        activity.setId(UUIDUtils.getUUID());
        activity.setCreatetime(DateUtils.date_ymd(new Date()));
        activity.setCreateby(user.getId());

        ReturnObject returnObject = new ReturnObject();
        try {
            int ret=activityService.saveActivity(activity);
            if(ret>0){
                returnObject.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setMessage("添加成功");
            }else{
                returnObject.setCode(Constant.RETURN_OBJECT_CODE_FAILED);
                returnObject.setMessage("系统忙，稍后重试。。。");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return returnObject;
    }

    @RequestMapping("/workbench/activity/queryActivityByConditionForPage.do")
    @ResponseBody
    public Object queryActivityByConditionForPage(String name,String owner,String startDate,
                                                  String endDate, int pageNo,int pageSize){
        System.out.println("****************query*******************");
        System.out.println("name: "+name+",owner:"+owner+",start:"+startDate+"end:"+endDate);
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        map.put("owner",owner);
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        map.put("beginNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);

        List<Activities> activities = activityService.queryActivityByConditionForPage(map);
        System.out.println("********List**********："+activities);
        int total_count = activityService.queryCountOfActivityByConditionForPage(map);
        System.out.println("********count********："+total_count);
        Map<String,Object> ret = new HashMap<>();
        ret.put("activityList",activities);
        ret.put("count",total_count);

        return ret;
    }

    @RequestMapping("/workbench/activity/deleteActivityByIds.do")
    @ResponseBody
    public Object deleteActivityByIds(String[] ids){
        ReturnObject ret = new ReturnObject();
        try {
            int rows = activityService.deleteActivityByIds(ids);
            if(rows>0){
                ret.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);
                ret.setMessage("成功删除"+rows+"条数据");
            }else{
                ret.setCode(Constant.RETURN_OBJECT_CODE_FAILED);
                ret.setMessage("删除失败...");
            }
        }catch (Exception e){
            e.printStackTrace();
            ret.setCode(Constant.RETURN_OBJECT_CODE_FAILED);
            ret.setMessage("删除失败...");
        }
        return ret;
    }

    @RequestMapping("/workbench/activity/queryActivityById.do")
    @ResponseBody
    public Object queryActivityById(String id){
        System.out.println("******************queryActivityById*******************");
        Activities activity = activityService.queryActivityById(id);

        ReturnObject ret = new ReturnObject();
        if(activity==null){
            ret.setCode(Constant.RETURN_OBJECT_CODE_FAILED);
            ret.setMessage("查询不到相关数据.");
        }else{
            ret.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

            Map<String, Object> ret_data = new HashMap<>();
            ret_data.put("owner_id",activity.getOwner());
            ret_data.put("name",activity.getName());
            ret_data.put("startDate",activity.getStartdate());
            ret_data.put("endDate",activity.getEnddate());
            ret_data.put("cost",activity.getCost());
            ret_data.put("description",activity.getDescription());

            ret.setRetData(ret_data);
        }

        return ret;
    }

    @RequestMapping("/workbench/activity/updateActivityById.do")
    @ResponseBody
    public Object updateActivityById(Activities activity,HttpSession session){
        User user = (User)session.getAttribute(Constant.SESSION_USER);
        activity.setEdittime(DateUtils.date_formate(new Date()));
        activity.setEditby(user.getId());

        ReturnObject ret = new ReturnObject();
        try {
            int rows = activityService.updateActivityById(activity);
            if(rows>0){
                ret.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);
            }else{
                ret.setMessage(Constant.RETURN_OBJECT_CODE_FAILED);
                ret.setMessage("更新失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            ret.setMessage(Constant.RETURN_OBJECT_CODE_FAILED);
            ret.setMessage("更新失败");
        }
        return ret;
    }
}
